package cn.stronglink.assetmanage.business.inspection.model;

import android.widget.RemoteViews;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.RepailDetailDao;
import cn.stronglink.assetmanage.dao.RepairDao;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.netrequest.business.repail.RepairService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 维修数据
 * Created by zhengjinglan on 2018/6/21.
 */

public class RepairModel {
    private RepairService repairService;
    private Gson gson = new Gson();
    private onRepairListener repairListener;

    public onRepairListener getRepairListener(){return repairListener;}

    public void setRepairListener(onRepairListener repairListener){
        this.repairListener = repairListener;
    }

    public RepairModel(){
        repairService = new RepairService();
    }

    /**
     * 获取维修单列表
     * @return
     */
    public List<Repair> getDataByRepairIdDistince(){
        List<Repair> repairList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().queryBuilder().distinct()
                .orderDesc(RepairDao.Properties.Id_).orderDesc(RepairDao.Properties.CreateTime).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (repairList != null) {
            return repairList;
        }
        return null;
    }

    /**
     * 获取维修数据单
     */
    public void getRepairData(){
        try {

            repairService.downloadRepairWorkOrder(User.getInstance().getId_(), new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (repairListener != null){
                        repairListener.onFailer(call, e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (repairListener != null){
                        repairListener.onResponse(call, response);
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 删除全部的维修单和维修单详情
     */
    public void deleteAllRepairData(){
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().deleteAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().deleteAll();
    }

    public interface onRepairListener{
        void onFailer(Call call , IOException ex);
        void onResponse(Call call, Response response);
    }
}
